package com.avalon.shop.controller;

import com.avalon.core.exception.AvalonException;
import com.avalon.core.model.Record;
import com.avalon.shop.service.CartService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("cart")
public class CartController {
    private final CartService cartService;

    public CartController(CartService cartService) {
        this.cartService = cartService;
    }

    @PostMapping("/add/product")
    public void addCart(@RequestBody Map<String, Object> param) {
        Integer productId = 0;
        Integer userId = 0;
        Integer count = 1;

        if (param.containsKey("productId")) {
            productId = (Integer) param.get("productId");
        }
        if (param.containsKey("userId")) {
            userId = (Integer) param.get("userId");
        }
        if (param.containsKey("count")) {
            count = Integer.parseInt(param.get("count").toString());
        }
        if (productId.equals(0) || userId.equals(0)) {
            throw new AvalonException("Invalid product or user id");
        }
        cartService.addCartProduct(productId, userId, count);
    }

    @PostMapping("/sub/product")
    public void subCart(@RequestBody Map<String, Object> param) {
        Integer productId = 0;
        Integer userId = 0;
        if (param.containsKey("productId")) {
            productId = (Integer) param.get("productId");
        }
        if (param.containsKey("userId")) {
            userId = (Integer) param.get("userId");
        }
        if (productId.equals(0) || userId.equals(0)) {
            throw new AvalonException("Invalid product or user id");
        }
        cartService.addCartProduct(productId, userId, -1);
    }

    @PostMapping("/product/get")
    public List<Map> getCartProductByUserId(@RequestBody Map<String, Object> param) {
        Integer userId = 0;
        if (param.containsKey("userId")) {
            userId = (Integer) param.get("userId");
        }
        return cartService.getCartProductByUserId(userId);
    }

    @PostMapping("/product/count")
    public Map<String, Object> getCartProductCountByUserId(@RequestBody Map<String, Object> param) {
        Integer userId = 0;
        if (param.containsKey("userId")) {
            userId = (Integer) param.get("userId");
        }
        Map<String, Object> result = new HashMap<>();
        Integer sum = cartService.getCartProductCountByUserId(userId);
        result.put("sum", sum);
        return result;
    }

    @PostMapping("/product/delete")
    public void deleteCartProduct(@RequestBody Map<String, Object> param) {
        Integer id = 0;
        if (param.containsKey("id")) {
            id = (Integer) param.get("id");
        }
        cartService.deleteCartProduct(id);
    }
}
